package com.cdm.util;

import java.util.Calendar;

import com.cdm.domain.service.SequenceService;
import com.cdm.domain.service.SequenceServiceImpl;
import com.cdm.persistence.Sequence;
import com.cdm.persistence.User;
import com.vivo.util.DTool;

public class IdGenerator {
	private static DTool D = new DTool(DTool.DEBUG);
	// CP/SP ID
	// 用户类别 待扩展 注册代理机构 CP/SP 注册年份
	// 2 2 8 20 8
	// 0000 ,0000 0000, 0000 0000 0000 0000 0000, 0000 0000
	// F-FF-FFFFF-FF
	public static final long USER_TYPE_MASK = 0xC000000000l;
	public static final long EXTENSTION_MASK = 0x3000000000l;
	public static final long REGISTERED_AGENT_MASK = 0x0FF0000000l;
	public static final long SERIAL_MASK = 0xC000FFFF00l;
	public static final long YEAR_MASK = 0xc0000000FFl;

	public static final long USER_TYPE_INDENT = 38l;
	public static final long EXTENSTION_INDENT = 36l;
	public static final long REGISTERED_AGENT_INDENT = 28l;
	public static final long SERIAL_INDENT = 8l;
	public static final long YEAR_INDENT = 0l;

	// 内容提供商时值为(00) 2，服务提供商时值为(01) 2
	public static final long USER_TYPE_CP = 0l << USER_TYPE_INDENT;
	public static final long USER_TYPE_SP = 1l << USER_TYPE_INDENT;
	public static final long USER_TYPE_CDM = 2l << USER_TYPE_INDENT;
	public static final long USER_TYPE_ADMIN = 3l << USER_TYPE_INDENT;
	public static final long USER_TYPE_END_USER = 4l << USER_TYPE_INDENT;

	public static final long EXTENSION = 0l << EXTENSTION_INDENT;
	public static final long REGISTERED_AGENT = 0l << REGISTERED_AGENT_INDENT;

	/**
	 * 
	 * 
	 * @return
	 */
	public static long generateUserId(int userType) {
		long userId = 0l;

		long sequence = 0l;

		switch (userType) {
		case User.ROLE_CP:
			userId |= USER_TYPE_CP;
			sequence = sequenceService.getSequence(Sequence.USER_CP_SEQUENCE);
			break;
		case User.ROLE_SP:
			userId |= USER_TYPE_SP;
			sequence = sequenceService.getSequence(Sequence.USER_SP_SEQUENCE);
			break;
		case User.ROLE_CDM:
			userId |= USER_TYPE_CDM;
			sequence = sequenceService.getSequence(Sequence.USER_CDM_SEQUENCE);
			break;
		case User.ROLE_ADMIN:
			userId |= USER_TYPE_ADMIN;
			sequence = sequenceService.getSequence(Sequence.USER_ADMIN_SEQUENCE);
			break;
		case User.ROLE_END_USER:
			userId |= USER_TYPE_END_USER;
			sequence = sequenceService.getSequence(Sequence.USER_ENDUSER_SEQUENCE);
			break;
		default:
			userId |= USER_TYPE_END_USER;
			sequence = sequenceService.getSequence(Sequence.USER_ENDUSER_SEQUENCE);
			break;
		}

		userId |= EXTENSION;
		userId |= REGISTERED_AGENT;
		// searial
		userId |= sequence << SERIAL_INDENT;
		// Year
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR) - 2000;
		userId |= year << YEAR_INDENT;
		DTool.d.debug("Generate User Id : " + Long.toBinaryString(userId));
		return userId;
	}

	// DCID
	// 版本号 提供者类别 注册代理机构 内容提供者 注册年份 内容编码
	// 3 1 8 20 8 24
	// 000 ,0 ,0000 0000,0000 0000 0000 0000 0000 , 0000 0000, 0000 0000 0000 0000 0000 0000
	// E|1,FF,FFFFF,FF,FFFFFF
	// 版本号 提供者类别 注册代理机构 内容提供者 注册年份 内容编码
	// 3 1 8 32 8 12
	// 000,0, 0000 0000, 0000 0000 0000 0000 0000 0000 0000 0000 , 0000 0000, 0000 0000 0000
	// E|1,FF,FFFFFFFF,FF,FFF
	public static final long DCID_VERSION_ID_MASK = 0xE000000000000000l;
	public static final long DCID_CP_TYPE_MASK = 0x1000000000000000l;
	public static final long DCID_REGISTERED_AGENT_MASK = 0x0FF0000000000000l;
	public static final long DCID_CP0_MASK = 0x000FFFFF00000000l;
	public static final long DCID_CP1_MASK = 0x000FFFFFFFF00000l;
	public static final long DCID_YEAR0_MASK = 0x00000000FF000000l;
	public static final long DCID_YEAR1_MASK = 0x00000000000FF000l;
	public static final long DCID_CONTENT_CODE0_MASK = 0x0000000000FFFFFFl;
	public static final long DCID_CONTENT_CODE1_MASK = 0x0000000000000FFFl;

	public static final long DCID_VERSION_ID_INDENT = 61l;
	public static final long DCID_CP_TYPE_INDENT = 60l;
	public static final long DCID_REGISTERED_AGENT_INDENT = 52l;
	public static final long DCID_CP0_INDENT = 32l;
	public static final long DCID_CP1_INDENT = 20l;
	public static final long DCID_YEAR0_INDENT = 24l;
	public static final long DCID_YEAR1_INDENT = 12l;
	public static final long DCID_CONTENT_CODE0_INDENT = 0l;
	public static final long DCID_CONTENT_CODE1_INDENT = 0l;

	// 内容提供商时值为(00) 2，服务提供商时值为(01) 2

	public static final long DCID_CP_LEGAL = 0l;
	public static final long DCID_CP_HUMAN = 1l;
	public static final long DCID_DEFAULT_VERSION_ID = 1l << DCID_VERSION_ID_INDENT;

	public static long generateDCID(long cp_type, long cp, long content_code) {

		// hack
		content_code = sequenceService.getSequence("content_code");
		// ~hack
		long dcid = 0l;
		dcid |= DCID_DEFAULT_VERSION_ID;

		if (cp_type >= 1) {
			cp_type = 1;
		}
		dcid |= cp_type << DCID_CP_TYPE_INDENT;

		// Year
		Calendar c = Calendar.getInstance();
		long year = new Integer(c.get(Calendar.YEAR) - 2000).longValue();

		if (cp_type == 0) {
			dcid |= cp << DCID_CP0_INDENT;
			dcid |= year << DCID_YEAR0_INDENT;
			dcid |= content_code << DCID_CONTENT_CODE0_INDENT;
		} else {
			dcid |= cp << DCID_CP1_INDENT;
			dcid |= year << DCID_YEAR1_INDENT;
			dcid |= content_code << DCID_CONTENT_CODE1_INDENT;
		}
		DTool.d.debug("Generate DCID : " + Long.toBinaryString(dcid));
		return dcid;
	}

	public static void main(String[] args) {

		Long.toBinaryString(generateDCID(0l, 0xeel, 0xffl));
		// for (int i = 0; i < 50; i++) {
		// System.out.println(i + " | " + Long.toBinaryString(1l << i));
		// }
	}

	private static SequenceService sequenceService = new SequenceServiceImpl();
	
	
	
	
	
	
	
	
	
	
	
	
}
